package com.yungu.swift.api.route.v2.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.utils.FileUploadUtil;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.system.sys.model.dto.SysAirportDto;
import com.yungu.swift.system.sys.service.SysAirportService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller(value = "commonApiV2")
@RequestMapping(value = "/api/v2/passenger/common")
@Api(value = "【乘客】通用模块接口", tags = "获取标签，帮助文档和开通业务的相关操作")
public class CommonApi {

    @Reference
    private SysAirportService sysAirportService;

    @ResponseBody
    @RequestMapping(value = {"/getAirport"}, method = RequestMethod.POST)
    @ApiOperation(value = "获取城市机场列表", notes = "获取城市机场列表", response = ResponseData.class)
    public ResponseData<List<SysAirportDto>> getAirport(@RequestBody Map<String, Object> params) {
        return sysAirportService.getAirportByCityName(params);
    }

    @ResponseBody
    @RequestMapping(value = {"/token/uploadComplaintImg"})
    @ApiOperation(value = "投诉上传图片接口", response = ResponseData.class)
    public ResponseData<Map<String, Object>> uploadPhoto(
//            @ApiParam(name = "appid", value = "APP标识", required = false) @RequestHeader String appid,
            @ApiParam(name = "appid", value = "APP标识") @RequestParam(required = false) String appid,
            @ApiParam(name = "images", value = "投诉图片", required = true) @RequestPart MultipartFile images,
            @ApiParam(name = "passengerMobile", value = "用户手机号码", required = true) @RequestPart String passengerMobile) {
        String fileName = images.getOriginalFilename();
        String lastName = StringUtils.substringAfter(fileName, ".");
        if (!lastName.equals(MagicConstant.PICTURE_SUFFIX_JPG) && !lastName.equals(MagicConstant.PICTURE_SUFFIX_PNG)
                && !lastName.equals(MagicConstant.PICTURE_SUFFIX_JPEG) && !lastName.equals(MagicConstant.PICTURE_SUFFIX_BMP)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "图片格式不正确，请重新选择！");
        }
        //上传货件图片
        ResponseData<String> responseData = FileUploadUtil.uploadImage(images, appid, passengerMobile, "complaintImg");
        if (!responseData.isSuccess() || StringUtils.isEmpty(responseData.getData())) {
            return ResponseData.buildErrorResponse(responseData.getErrCode(), responseData.getMsg());
        }
        return ResponseData.buildSuccessResponse("上传成功", MapUtils.build("imagesPath", responseData.getData()));
    }
}
